var cartMain = document.getElementsByClassName("cart-main")[0];
var cartWrap = document.getElementsByClassName("cart-wrap")[0];
var checkAll = document.getElementsByClassName("check-all")[0];
var checkOneList = document.getElementsByClassName("check-one");
var addList = document.getElementsByClassName("add");
var reduceList = document.getElementsByClassName("reduce");
var colSubtotalList = document.getElementsByClassName("col-subtotal");
var colPriceList = document.getElementsByClassName("col-price");
var countSpanList = document.getElementsByClassName("countSpan");
var reduceList = document.getElementsByClassName("reduce");
var colOperateList = document.getElementsByClassName("col-operate");
var listItemList = document.getElementsByClassName("list-item");
var deleteList = document.getElementsByClassName("delete");
var totalNum = document.getElementsByClassName("totalNum")[0];
var selectedNum = document.getElementsByClassName("selectedNum")[0];
var selectedTotal = document.getElementsByClassName("selectedTotal")[0];
var colTotal = document.getElementsByClassName("col-total")[0];




cartWrap.onclick = function(e){
    var e = e || window.event;
    var target = e.target || e.srcElement;

    //全选控制单选
    if (target.classList.contains("check-all")) {
        var status = target.checked;
        for (var i = 0; i < checkOneList.length; i++) {
            var checkOne = checkOneList[i];
            checkOne.checked = status;
        }

        //结算
        settlement();
    }

    //单选影响全选
    else if (target.classList.contains("check-one")) {
        checkAll.checked = [...checkOneList].every(item => item.checked == true);  
        
        //结算
        settlement();
    }

    //实现数量的加
    else if (target.className == "add") {
        var index = Array.from(addList).indexOf(target);

        var num = countSpanList[index].textContent;
        num++;
        countSpanList[index].textContent = num;

        var cost = parseInt(colPriceList[index + 1].textContent);
        colSubtotalList[index + 1].textContent = cost * num + "元";

        //结算
        settlement();
    }

    //实现数量的减
    else if(target.className=="reduce"){
        var index = Array.from(reduceList).indexOf(target);

        var num = countSpanList[index].textContent;
        if(num == 1)return false;
        num--;
        countSpanList[index].textContent = num;
        var cost = parseInt(colPriceList[index + 1].textContent);
        colSubtotalList[index + 1].textContent = cost * num + "元";

        //结算
        settlement();
    }

    //实现删除
    else if(target.classList.contains("delete")){
        var index = Array.from(deleteList).indexOf(target);
        
        if(confirm("是否删除该商品?")){
            listItemList[index].remove()
        }

        //删除之后判断是否全选
        checkAll.checked = [...checkOneList].every(item => item.checked == true);
        if(checkOneList.length == 0){
            checkAll.checked = false;
        }

        //结算
        settlement();
    }

    // 结算
    //共 件  已选 件    合计  元
    function settlement(){
        //共 件
        totalNum.textContent = listItemList.length;


        // [{ele:checkOne,index:}]

        //已选 件
        var alreadyList = Array.from(checkOneList).filter(function(item){
            return item.checked == true;
        })
        selectedNum.textContent = alreadyList.length;

        //合计  元
        var money = 0;
        for(var i = 0 ; i < checkOneList.length ; i++){
            var checkone = checkOneList[i].checked;
            if(checkone == true){
                var num = countSpanList[i].textContent;
                var cost = parseInt(colPriceList[i + 1].textContent);
                var product = num * cost;
                money += product;
            }
        }
        console.log(money);
        selectedTotal.textContent = money;
    }
}